<?php
namespace TennisTournament\Persistent;

use Doctrine\Common\Collections\Criteria;
use TennisTournament\Model\Security;
use Doctrine\ORM\EntityManager;

class AuthenticationDaoDoctrine extends GenericDaoDoctrine implements AuthenticationDao {

    const SECURITY_ENTITY = "TennisTournament\Model\Security";

    public function __construct(EntityManager $entityManager) {
        parent::__construct($entityManager, self::SECURITY_ENTITY);
    }

    /**
     * Get {@link Security} by $username and $password
     * @param $username
     * @param $password
     * @return Security or null if havent matching
     */
    public function getBy($username, $password) {
        $entityRepository = $this->entityManager->getRepository(self::SECURITY_ENTITY);
        $criteria = Criteria::create()
            ->where(Criteria::expr()->eq("login", $username))
            ->andWhere(Criteria::expr()->eq("password", $password));

        /** @var Security */
        return $entityRepository->matching($criteria)->first();
    }

}